package com.ck.ips.core.config;

import com.ck.ips.core.interceptor.ResourceInterceptor;
import com.ck.ips.core.interceptor.UserContextInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
	@Autowired
	private CustomProperties customProperties;

	// 拦截器注册
	@Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userContextInterceptor());
		registry.addInterceptor(resourceInterceptor());
    }

	/**
	 * 判断用户是否登录拦截器
	 */
    @Bean
	public UserContextInterceptor userContextInterceptor(){
		return new UserContextInterceptor();
	}

	/**
	 * 判断请求是否合法拦截器
	 */
	@Bean
	public ResourceInterceptor resourceInterceptor(){
		return new ResourceInterceptor();
	}


	/**
	 * 跨域支持
	 * @param registry
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
				.allowedOrigins(customProperties.getStaticResourceDomain().split(","))
				.allowedOrigins("*")
				.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
				.maxAge(3600)
				.allowCredentials(true);
	}
}
